package com.fangcun.controller.admin.old;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseAdminController;
import com.fangcun.entity.Goods;
import com.fangcun.entity.GoodsPrice;
import com.fangcun.entity.User;
import com.fangcun.service.admin.GoodsSeriver;
import com.fangcun.service.admin.GoodsCategoryService;

/**后台-产品**/
@RestController
@RequestMapping("/admin/{version}/auth/goods")
public class GoodsController extends BaseAdminController{
	
	private static Logger LOG = LoggerFactory.getLogger(GoodsController.class);
	
	@Autowired
	private GoodsSeriver goodsSeriver;
	@Autowired
	private GoodsCategoryService categroyService;
	
	@ApiVersion(1.0)
	@RequestMapping("listWithChildren")
	private ResponseMsg listWithChildren() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if(getTeahouseId() == null) {
				msg.setData(categroyService.listWithChildren(null));
			}else {
				msg.setData(categroyService.listWithChildren(getTeahouseId()));
			}
		}catch (ProException e) {
			LOG.error("-针对店铺-商品分类下拉出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-针对店铺-商品分类下拉异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "list")
	private ResponseMsg list(@RequestBody PageBean<Goods> request) {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			request.getParam().setTeahouseId(getTeahouseId());
			msg.setData(goodsSeriver.list(request));
		} catch (ProException e) {
			LOG.error("-产品列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return  msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "add")
	private ResponseMsg add(@RequestBody Goods request) {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goodsSeriver.add(request));
		} catch (ProException e) {
			LOG.error("-产品增加出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品增加异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return  msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "update")
	private ResponseMsg update(@RequestBody Goods request) {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			goodsSeriver.update(request);
		} catch (ProException e) {
			LOG.error("-产品修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品修改异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "/detail")
	private ResponseMsg details(Long id) {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goodsSeriver.detail(id));
		} catch (ProException e) {
			LOG.error("-产品详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "price/list")
	private ResponseMsg listPrice(@RequestBody PageBean<GoodsPrice> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goodsSeriver.listPrice(request));
		} catch (ProException e) {
			LOG.error("-产品套餐列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品套餐列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	@ApiVersion(1.0)
	@RequestMapping(value = "price/add")
	private ResponseMsg priceAdd(@RequestBody GoodsPrice request) {
		ResponseMsg msg= ResponseMsg.returnBeanBuild();
		try {
			goodsSeriver.addPrice(request);
		} catch (ProException e) {
			LOG.error("-产品套餐增加出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品套餐增加异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "price/update")
	private ResponseMsg priceUpdate(@RequestBody GoodsPrice request) {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			goodsSeriver.updatePrice(request);
		} catch (ProException e) {
			LOG.error("-产品修改价格出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品修改价格异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "price/list/update")
	private ResponseMsg priceUpdate(@RequestBody List<GoodsPrice> request) {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			goodsSeriver.updateListPrice(request);
		} catch (ProException e) {
			LOG.error("-产品修改价格出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品修改价格异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "price/details")
	private ResponseMsg priceDetails(Long id) {
		ResponseMsg msg=ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goodsSeriver.detailsPrice(id));
		} catch (ProException e) {
			LOG.error("-产品套餐详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-产品套餐详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/**分销商下拉**/
	@ApiVersion(1.0)
	@RequestMapping(value = "dropdown")
	private ResponseMsg userDropDown (@RequestBody User request) {
		ResponseMsg msg= ResponseMsg.returnBeanBuild();
		try {
			request.setUserType(2);
			msg.setData(goodsSeriver.userDropDown(request));
		} catch (ProException e) {
			LOG.error("-分销商下拉出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-分销商下拉异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
